home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1996 September / MACPOWER-1996-09.ISO.7z / MACPOWER-1996-09.ISO / 第2特集:プラグイン大集合 / PIC2 Save / pic2.h < prev    next >
Text File  |  1995-01-16  |  9KB  |  332 lines

  1. /*
  2.  *
  3.  * PIC2 ヘッダーファイル by やなぎさわ
  4.  *
  5.  *    for  X680x0/MS-DOS/GO32 Modifyed Noboru Abe
  6.  *    for  RUN386/EXE386 Modifyed メルキュール
  7.  *    for  8bit color test Modifyed やなぎさわ
  8.  *    int->long
  9.  */
  10. #pragma once
  11.  
  12. #if 0
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stddef.h>
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <time.h>
  21. #include <string.h>
  22. #include <io.h>
  23. #endif
  24.  
  25. #ifndef _PIC2_INCLUDED
  26. #define _PIC2_INCLUDED
  27.  
  28. #if    defined( __HUMAN68K__) && !defined( __X68__)
  29. #    define __X68__
  30. #endif
  31.  
  32. #ifdef    __X68__
  33. #    define BIG_ENDIAN
  34. #endif
  35.  
  36. #if defined(__THINK__) || defined(__MWERKS__)
  37. #    define PADDING_STRUCT        /* 構造体に隙間ができる時に定義 */
  38. #define BIG_ENDIAN
  39. #define _HUGE_
  40. #include <string.h>
  41. #include <setjmp.h>
  42. #define memcpy(d,s,n) BlockMove((s),(d),(n))
  43.  
  44. #endif
  45.  
  46. #ifdef __GO32__
  47. #    define PADDING_STRUCT        /* 構造体に隙間ができる時に定義 */
  48. #endif
  49.  
  50. #ifdef __RUN386__
  51. #    pragma On(Pointers_compatible)
  52. #    pragma Off(Char_default_unsigned)
  53. #    include    <fcntl.h>
  54. #endif
  55.  
  56. #ifdef __GO32__
  57. #    include    <unistd.h>
  58. #endif
  59.  
  60. #ifdef __MWERKS__
  61. #            define    HALLOC(a,b)    NewPtrClear((long)a*b)
  62. #            define    HFREE(a)    DisposePtr(a)
  63. #endif
  64.  
  65. #ifdef _DOS_
  66. #    include <fcntl.h>
  67. #    ifdef _MSC_VER
  68. #        include    <malloc.h>
  69. #        define    _HUGE_        _huge
  70. #        define    HALLOC(a,b)    halloc(a,b)
  71. #        define    HFREE(a)    hfree(a)
  72. #    else
  73. #        ifdef  __TURBOC__
  74. #            include    <alloc.h>
  75. #            define    _HUGE_        far
  76. #            define    HALLOC(a,b)    farcalloc(a,b)
  77. #            define    HFREE(a)    farfree(a)
  78. #        else
  79. #            define    _HUGE_
  80. #            define    HALLOC(a,b)    malloc((long)a*b)
  81. #            define    HFREE(a)    free(a)
  82. #        endif /* __TURBOC__ */
  83. #    endif /* _MSC_VER */
  84. #else
  85. #if 0
  86. #    define    _HUGE_
  87. #    ifdef    _RUN386__
  88. #        include    <fcntl.h>
  89. #    else
  90. #        include    <sys/fcntl.h>
  91. #    endif /* _HUGE_ */
  92. #endif
  93. #endif /* _DOS_ */
  94.  
  95. #ifdef _PIC2_
  96. #    define    EXT
  97. #else
  98. #    define    EXT    extern
  99. #endif
  100.  
  101. /* 簡単な型の定義 (環境によっては定義済みの可能性あり) */
  102. #ifndef UCHAR
  103. #    define UCHAR
  104.     typedef    unsigned char uchar;
  105.     typedef    signed char schar;            /* 符号付きchar            */
  106.     typedef    unsigned short ushort;        /* これは結構定義済みかな?    */
  107.     typedef    unsigned long ulong;
  108. #endif
  109.  
  110. typedef    ulong pix;            /* 画素ね */
  111.  
  112. /*
  113.  * pic2libのエラーコード
  114.  *(errnoと重ならないようにすること8bit以上なので終了コードに使うのは注意 :-)
  115.  */
  116. #define    P2E_FEOF    257        /* EOFを超えてファイルを読み込もうとした    */
  117. #define    P2E_BADFORM    258        /* フォーマットが未知または不正または異常    */
  118.  
  119. /* PIC2ファイルのオープンモード */
  120. #define    P2_OPEN_READ    0
  121. #define    P2_OPEN_WRITE    1
  122.  
  123. /* その他の定義 */
  124. #define    N_FBUF    (8*1024)    /* ファイルバッファのサイズ                    */
  125.  
  126. #ifndef TRUE
  127. #    define    TRUE    1
  128. #    define    FALSE    0
  129. #endif
  130.  
  131. /* 変更してはいけない定義 (ここだけ変えても旨くいかない ^^;)        */
  132. #define    COLOR_CACHE_BITS    5
  133. #define N_COLOR_CACHE    ((1<<COLOR_CACHE_BITS) - 1)    /* 算術版の色キャッシュサイズ */
  134. #define N_CONTEXT        128    /* 算術版の確率空間数                     */
  135. #define CACHE_BITS        6
  136. #define N_CACHE            (1<<CACHE_BITS)    /* 高速版の色キャッシュサイズ */
  137.  
  138. #ifdef    BIG_ENDIAN
  139. #    define    SHORT    short
  140. #    define    LONG    long
  141.  
  142. #    define    SHORT2short( n)    (n)
  143. #    define    LONG2long( n)    (n)
  144. #    define    short2SHORT( n)    (n)
  145. #    define    long2LONG( n)    (n)
  146.  
  147. #    define    mem2short( addr)    (*(short *)(addr))
  148. #    define    mem2long( addr)        (*(long *)(addr))
  149. #else
  150.     typedef    short    SHORT;
  151.     typedef    long    LONG;
  152.  
  153.     extern    short    SHORT2short( SHORT n);
  154.     extern    long    LONG2long( LONG n);
  155.     extern    SHORT    short2SHORT( short n);
  156.     extern    LONG    long2LONG( long n);
  157.  
  158. #    define    mem2short( addr)    SHORT2short(*(SHORT *)(addr))
  159. #    define    mem2long( addr)        LONG2long(*(LONG *)(addr))
  160. #endif
  161.  
  162.  
  163. /* PIC2 のヘッダ イメージ (パディング無しで詰まっていなくてはいけない)*/
  164. struct p2_hdr {
  165. /*   0 */    uchar magic[4];        /* ヘッダID "P2DT"                    */
  166. /*   4 */    uchar name[18];        /* 作者名                            */
  167. /*  22 */    uchar subtitle[8];    /* セーブ時のファイル名先頭8文字     */
  168. /*  30 */    uchar crlf0[2];        /* CRLF                            */
  169. /*  32 */    uchar title[30];    /* タイトル名                        */
  170. /*  62 */    uchar crlf1[2];        /* CRLF                            */
  171. /*  64 */    uchar saver[30];    /* セーバー名                        */
  172. /*  94 */    uchar crlf2[2];        /* CRLF                            */
  173. /*  95 */    uchar eof;            /* 終端                                */
  174. /*  97 */    uchar reserve0;        /* 空白エリア                        */
  175. /*  98 */    SHORT flag;            /* FLG                            */
  176. /* 100 */    SHORT no;            /* 作品番号                            */
  177. /* 102 */    LONG time;            /* 日付                                */
  178. /* 106 */    LONG size;            /* ヘッダサイズ                        */
  179. /* 110 */    SHORT depth;        /* 色数(ビット深さ)                    */
  180. /* 112 */    SHORT x_aspect;        /* 横方向ドット比率                    */
  181. /* 114 */    SHORT y_aspect;        /* 縦方向ドット比率                    */
  182. /* 116 */    SHORT x_max;        /* 画像最大サイズX                    */
  183. /* 118 */    SHORT y_max;        /* 画像最大サイズY                    */
  184. /* 120 */    long reserve1;        /* 空白エリア                        */
  185. /* uchar pal[][3]; パレットとコメントはサイズ不定なので別    */
  186. /* uchar comment;    */
  187. };
  188.  
  189. #define SIZE_OF_HEADER    124
  190.  
  191. /* ブロックのヘッダ イメージ */
  192. struct p2_blk {
  193. /*   0 */    uchar id[4];        /* 圧縮ID                            */
  194. /*   4 */    LONG size;            /* サイズ                            */
  195. /*   8 */    SHORT flag;            /* フラグ                            */
  196. /*  10 */    SHORT x_wid;        /* 画像サイズX                        */
  197. /*  12 */    SHORT y_wid;        /* 画像サイズY                        */
  198. /*  14 */    SHORT x_offset;        /* オフセットX                        */
  199. /*  16 */    SHORT y_offset;        /* オフセットY                        */
  200. /*  18 */    LONG opaque;        /* 透明色                            */
  201. /*  22 */    LONG reserve;        /* 空きエリア                        */
  202. };
  203.  
  204. #define SIZE_OF_BLK        26
  205.  
  206. /* PIC2 の各種ルーチンのハンドラ */
  207. typedef struct _P2 {
  208.     struct p2_hdr header;        /* ヘッダイメージ                    */
  209.     struct p2_blk blk;            /* ブロックヘッダ                    */
  210.     short n_pal;                /* パレット数                        */
  211.     short pal_bits;                /* パレット有効ビット数                */
  212.     uchar pal[256][3];            /* パレット格納場所                    */
  213.     uchar *comment;                /* コメント                            */
  214.     short fds;                    /* ファイルでぃすくりぷた            */
  215.     uchar mode;                    /* 'r'=read/'w'=write モード        */
  216.     long macbin_offset;            /* macバイナリのオフセット量        */
  217.     short errno;                /* エラーが入る
  218.                                  * <  0x100 : <errno.h>と同じ
  219.                                  * >= 0x100 : pic2独自のエラー
  220.                                  */
  221.     long next_pos;                /* 次のブロックの位置(load時)        */
  222.     long blk_pos;                /* 現loadブロックの位置                */
  223.     short x_max;                /* 最大幅                            */
  224.     short y_max;                /* 最大高                            */
  225.  
  226.     long ynow;                    /* 展開/圧縮中のライン                */
  227.     uchar *buff;                /* ユーザが与えたバッファ
  228.                                  * ここがNULLだとこっちでmallocする
  229.                                  */
  230.     long n_buff;                /* バッファのサイズ                    */
  231.  
  232.     /* 展開/圧縮に使う各種バッファ */
  233.  
  234.     pix *vram_prev;
  235.     pix *vram_now;
  236.     pix *vram_next;
  237.     schar *flag_now;
  238.     schar *flag_next;
  239.     schar *flag2_now;
  240.     schar *flag2_next;
  241.     schar *flag2_next2;
  242.     pix   (_HUGE_ * cache)[N_COLOR_CACHE + 1];
  243.     ushort *cache_pos;
  244.     ushort *mulu_tab;
  245.  
  246.     /* 展開/圧縮に使うファイルバッファと算術部のパラメータ */
  247.  
  248.     uchar fbuf[N_FBUF];
  249.     uchar *fbufp;
  250.     long n_fbuf;
  251.     ushort bit_buf;
  252.     short n_bit_buf;
  253.     long aa;
  254.     long cc;
  255.     long dd;
  256.     char cache_hit_c;    /* 前回色キャッシュにヒットしてるか?
  257.                          * 15    当たり/ 16    外れ
  258.                          * (15/16は算術圧縮の空間番号ね)
  259.                          */
  260.     /* 展開/圧縮に使う関数へのポインタ */
  261.     long (*nextline)( struct _P2 *p2, pix **line);
  262.  
  263.     /* 他に取っておきたい変数を入れるところ */
  264.     pix        (* func)(pix );
  265.     long    data;
  266. } P2;
  267.  
  268. /* メモリの確保と開放関係 */
  269.  
  270. #define NEW( m)        ((m) = mem_alloc( sizeof( *(m))))
  271. #if __MWERKS__
  272. #define FREE( m)    { if ( m != NULL) { DisposePtr((Ptr) m); m = NULL; } }
  273. #else
  274. #define FREE( m)    { if ( m != NULL) { free( m); m = NULL; } }
  275. #endif
  276.  
  277. /* 各種ワークなど    */
  278.  
  279. EXT short p2errno;        /* エラーの原因が入る            */
  280. EXT char raw_beta;        /* べた時のラインバッファを 0=他と同じ/1=ベタ    */
  281.  
  282. /* プロトタイプ宣言 */
  283.  
  284. void    l_memset( void  *mem, uchar data, long n);
  285.  
  286. long    read_file( P2 *p2, void *mem, long size);
  287. long    read_file2( P2 *p2, void *mem, long size);
  288. uchar    read_char( P2 *p2);
  289. ushort    read_short( P2 *p2);
  290. long    write_file( P2 *p2, void *mem, long size);
  291. long    write_char( P2 *p2, char a);
  292. long    write_short( P2 *p2, short n);
  293. long    write_long( P2 *p2, long n);
  294. long    seek_file( P2 *p2, long pos);
  295. long    tell_file( P2 *p2);
  296.  
  297. void    p2setpal( short p_n_pal, short p_pal_bits, uchar p_pal[][3]);
  298. void    p2setenv( char *savername, short ncolor, long xmax, long ymax, short xasp
  299.                 , short yasp, short rawbeta);
  300. void    p2setbuff( P2 *p2, long *buff, long n_buff);
  301. #ifdef __MWERKS__
  302. P2        *p2creat( FSSpec *fname, char *title, char *comment, char *name, short no);
  303. P2        *p2apend( FSSpec *fname, char *title, char *comment, char *name, short no);
  304. P2        *p2open( FSSpec *fname);
  305. #else
  306. P2        *p2creat( char *fname, char *title, char *comment, char *name, short no);
  307. P2        *p2apend( char *fname, char *title, char *comment, char *name, short no);
  308. P2        *p2open( char *fname);
  309. #endif
  310. short     p2close( P2 *p2);
  311. short    p2next( P2 *p2);
  312. short    p2find( P2 *p2);
  313. short    p2load( P2 *p2);
  314. long    p2save( P2 *p2, pix **line, long x, long y, long xw, long yw, char *id, pix opaque);
  315. long    p2nline( P2 *p2, pix **line);
  316. void    p2error( char *s);
  317.  
  318. long    p2sf_sv_init( P2 *p2, pix **line);
  319. long p2b_sv_init( P2 *p2, pix **line);
  320. long p2ss_sv_init( P2 *p2, pix **line);
  321. long p2ss_ld_init( P2 *pp2);
  322. long p2b_ld_init( P2 *p2);
  323. long p2sf_ld_init( P2 *p2);
  324.  
  325. long line_press( P2 *pp2, pix **line);
  326. short bit_decode(long c);
  327. short nn_decode( long c);
  328. pix read_color15( pix x);
  329. pix read_color24( pix x);
  330.  
  331. #endif    /* _PIC2_INCLUDED */
  332.